package com.ym.controller.doctor.work;

import com.github.pagehelper.PageInfo;
import com.ym.common.constant.Constants;
import com.ym.core.annotation.ApiSign;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.shiro.ShiroUtil;
import com.ym.medical.domain.param.CouponParam;
import com.ym.medical.domain.vo.CouponVo;
import com.ym.medical.domain.vo.FollowupVo;
import com.ym.medical.domain.vo.MyPatientVO;
import com.ym.medical.service.IAddressGroupSV;
import com.ym.medical.service.ICouponReceiveRecordSV;
import com.ym.medical.service.ICouponSV;
import com.ym.medical.service.IFollowupSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author: 张文博
 * @version: 2019年08月30日 17:18
 */
@Controller
@ApiSign
@Api("医生优惠券以及随访相关")
@RequestMapping(value = "/doctor/work",headers = Constants.Api.VERSION)
public class CouponController extends BaseController {

	@Autowired
	private ICouponSV iCouponSV;

	@Autowired
	private IFollowupSV iFollowupSV;

	@Autowired
	private IAddressGroupSV iAddressGroupSV;

	@Autowired
	private ICouponReceiveRecordSV receiveRecordSV;


	@ApiOperation("所有分组患者(粉丝)列表")
	@GetMapping("/follow/patientList")
	@ResponseBody

	public APIResponse followPatientList(
			@ApiParam(name = "pageNum", value = "页码", required = false)
			@RequestParam(name = "pageNum", required = true, defaultValue = "1")
					int pageNum,
			@ApiParam(name = "pageSize", value = "每页数量", required = false)
			@RequestParam(name = "pageSize", required = false, defaultValue = "10")
					int pageSize,
			@ApiParam(name = "keyword", value = "关键字")
			@RequestParam(required = false) String keyword) {
		Long currId = ShiroUtil.getLoginId();
		PageInfo<MyPatientVO> groupFriends =
				iAddressGroupSV.followPatientList(pageNum, pageSize, currId, keyword);
		return successData(groupFriends);

	}


	@ApiOperation("优惠券列表")
	@GetMapping("/coupon/list")
	@ResponseBody
	public APIResponse<PageInfo<CouponVo>> couponList(
			@ApiParam(name = "pageNo", value = "页码", required = true)
			@RequestParam(name = "pageNo", required = true, defaultValue = "1")
					int pageNo,
			@ApiParam(name = "pageSize", value = "每页数量", required = false)
			@RequestParam(name = "pageSize", required = false, defaultValue = "10")
					int pageSize,
			@ApiParam(name = "couponModel", value = "model对象 toSb  01图文问诊 02电话问诊 03视频问诊 isSys 01 医生 02 平台", required = false)
					CouponParam couponModel)throws Exception {
		//区别医生患者端调用
		Long currId = ShiroUtil.getLoginId();
		//Long currId = 116L;
		couponModel.setCurrId(currId);
		//小程序金币兑换
		if ("01".equals(couponModel.getType())) {
			couponModel.setTheType("03");
			PageInfo<CouponVo> couponPage = iCouponSV.couponList(pageNo, pageSize, couponModel);
			return successData(couponPage);
			//app 优惠券列表
		} else if ("02".equals(couponModel.getType())) {
			PageInfo<CouponVo> couponPage = iCouponSV.couponList(pageNo, pageSize, couponModel);
			return successData(couponPage);
			//小程序我的优惠券
		} else if ("03".equals(couponModel.getType())) {
			PageInfo<CouponVo> myCoupon = iCouponSV.myCoupon(pageNo, pageSize, couponModel);
			return successData(myCoupon);
		} else {
			return failMsg("请求参数有误");
		}

	}


	@ApiOperation("医生随访")
	@PostMapping("/doctor/follow")
	@ResponseBody
	public APIResponse patientFollow(
			@ApiParam(name = "followupVo", value = "随访对象", required = true)
			@RequestBody FollowupVo followupVo) {
		Long currId = ShiroUtil.getLoginId();
		int rows = iFollowupSV.saveFollowup(followupVo, currId);
		if (rows == 0) {
			return successMsg("操作失败");
		}
		return successMsg("成功随访" + followupVo.getPatientIds().size() + "患者");
	}

	@ApiOperation("医生随访前判断两周内记录")
	@PostMapping("/follow/record")
	@ResponseBody
	public APIResponse TowWeekRecord(
			@ApiParam(name = "ids", value = "患者id 数组", required = true)
			@RequestBody Long[] ids) {

		List<String> names = receiveRecordSV.TowWeekRecord(ids);
		if (names == null || names.isEmpty()) {
			return successData(true);
		} else {
			return successData(names);
		}

	}


}
